<template>
	<view class="content">
		<view class="infor-content">
			<view class="one-infor" style="margin-top:0;">
				<view class="title">上傳身份證</view>
				<view class="upload">
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="1" width="315" height="200">
						<view class="upload-content">
							<image src="../../static/image/index/add.png" mode=""></image>
							<text>身份證人像面</text>
						</view>
					</u-upload>
					<u-upload :fileList="fileList2" @afterRead="afterRead" @delete="deletePic" name="2" multiple
						:maxCount="1" width="315" height="200">
						<view class="upload-content">
							<image src="../../static/image/index/add.png" mode=""></image>
							<text>身份證國徽面</text>
						</view>
					</u-upload>
				</view>
			</view>
			<view class="one-infor">
				<view class="title">上傳駕駛證</view>
				<view class="upload">
					<u-upload :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
						:maxCount="1" width="315" height="200">
						<view class="upload-content">
							<image src="../../static/image/index/add.png" mode=""></image>
							<text>上傳駕駛證主頁</text>
						</view>
					</u-upload>
					<u-upload :fileList="fileList5" @afterRead="afterRead" @delete="deletePic" name="5" multiple
						:maxCount="1" width="315" height="200">
						<view class="upload-content">
							<image src="../../static/image/index/add.png" mode=""></image>
							<text>上傳駕駛證副頁</text>
						</view>
					</u-upload>
				</view>
			</view>
			<view class="one-infor" v-if="driverType==1">
				<view class="title">上傳的士司機證</view>
				<view class="upload">
					<u-upload :fileList="fileList4" @afterRead="afterRead" @delete="deletePic" name="4" multiple
						:maxCount="1" width="315" height="200">
						<view class="upload-content">
							<image src="../../static/image/index/add.png" mode=""></image>
							<text>上傳的士司機證</text>
						</view>
					</u-upload>
				</view>
			</view>
		</view>
		<view class="upLoad-btn" @click="toUploadInfor">
			提交
		</view>
	</view>
</template>

<script>
	import {
		approveDrive
	} from '../../request/api.js'
	export default {
		data() {
			return {
				fileList1: [],
				fileList2: [],
				fileList3: [],
				fileList4: [],
				fileList5: [],
			};
		},
		computed: {
			driverType() {
				return this.$store.state.user.driverType
			},
			imgUrl() {
				return this.$store.state.user.imgUrl
			}
		},
		methods: {
			async toUploadInfor() {
				if (this.fileList1.length == 0) {
					uni.showToast({
						title: '請上傳身份證正面照',
						icon: 'none'
					})
					return
				}
				if (this.fileList2.length == 0) {
					uni.showToast({
						title: '請上傳身份證背面照',
						icon: 'none'
					})
					return
				}
				if (this.fileList3.length == 0) {
					uni.showToast({
						title: '請上傳駕駛證主頁',
						icon: 'none'
					})
					return
				}
				if (this.fileList5.length == 0) {
					uni.showToast({
						title: '請上傳駕駛證副頁',
						icon: 'none'
					})
					return
				}
				if (this.driverType == 1 && this.fileList4.length == 0) {
					uni.showToast({
						title: '請上傳的士司機證',
						icon: 'none'
					})
					return
				}
				uni.showLoading({
					title: "上傳中..."
				})
				let res = await approveDrive({
					driver_type: this.driverType,
					idcard_image1: this.fileList1[0].url,
					idcard_image2: this.fileList2[0].url,
					driving_licence_image1: this.fileList3[0].url,
					driving_licence_image2: this.fileList5[0].url,
					taxi_document_image: this.fileList4[0].url
				})
				uni.hideLoading()
				if (res.code == 1) {
					uni.showToast({
						title: '上傳成功',
						icon: 'none'
					})
					this.fileList1 = [];
					this.fileList2 = [];
					this.fileList3 = [];
					this.fileList4 = [];
					this.fileList5 = [];
					uni.navigateBack()
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			// 新增图片 这个是上传图像的方法
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				// 用于存储符合条件的图片
				let Images = [];
				// 遍历上传的每张图片
				for (let i = 0; i < lists.length; i++) {
					const item = lists[i];
					Images.push(item)
				}
				let fileListLen = this[`fileList${event.name}`].length
				Images.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < Images.length; i++) {
					const result = await this.uploadFilePromise(Images[i].url)
					// 返回给后端服务器的 结果需要赋值给 this.infoForm.image = result
					console.log(result, '上传图像result');
					// this.infoForm.avatar = result
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: result.status,
						message: result.status == 'failed' ? '上传失败' : '',
						url: result.url
					}))
					fileListLen++
				}
				console.log(this.fileList1);
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: this.imgUrl + '/driver/common/upload', //后端接口地址
						filePath: url,
						name: 'file', //传给后端参数
						header: {
							'token': uni.getStorageSync('token')
						},
						formData: {
							path: 'avatar/user'
						},
						success: (res) => {
							console.log(res);
							if (res.statusCode == 200) {
								resolve({
									url: JSON.parse(res.data).data.url,
									status: 'success'
								})
							} else {
								resolve({
									url: url,
									status: 'failed'
								})
							}
						}
					});
				})
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FAFAFA;

		.content {
			width: 702rpx;
			margin-left: auto;
			margin-right: auto;
			padding-top: 24rpx;

			.upLoad-btn {
				width: 650rpx;
				height: 98rpx;
				background-color: #040404;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
				color: #ffffff;
				border-radius: 98rpx;
				position: absolute;
				bottom: 50rpx;
				left: 50rpx;
			}

			.infor-content {
				width: 100%;
				background-color: #ffffff;
				padding: 24rpx;
				box-sizing: border-box;
				box-shadow: 0 2rpx 12rpx 0 rgba(204, 204, 204, 0.3);

				.one-infor {
					margin-top: 24rpx;

					.title {
						font-size: 30rpx;
						color: #040404;
						line-height: 42rpx;
					}

					.upload {
						display: flex;
						justify-content: space-between;
						margin-top: 24rpx;

						.upload-content {
							width: 315rpx;
							height: 200rpx;
							background-color: #F6F6F6;
							border-radius: 15rpx;
							display: flex;
							align-items: center;
							justify-content: center;
							flex-direction: column;

							image {
								width: 50rpx;
								height: 50rpx;
							}

							text {
								margin-top: 10rpx;
								font-size: 20rpx;
								color: #9E9E9E;
								line-height: 28rpx;
							}
						}
					}
				}
			}
		}
	}
</style>